package cn.thr.ymcc.web.controller;

import cn.thr.ymcc.constant.Cos;
import cn.thr.ymcc.domain.MediaFile;
import cn.thr.ymcc.service.IMediaFileService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import springfox.documentation.spring.web.json.Json;

import java.io.UnsupportedEncodingException;

@Slf4j
@Component
@RocketMQMessageListener(
        consumerGroup = "VideoPushStreamConsumer",
        topic = Cos.VideoStream.TOPIC_COURSE_VIDEO_PUSH_STREAM,
        selectorExpression = Cos.VideoStream.TAGS_COURSE_VIDEO_PUSH_STREAM
)
public class VideoPushStreamConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private IMediaFileService mediaFileService;

    @Override
    public void onMessage(MessageExt messageExt) {
        try {
            log.info("媒体中心消费者正在介绍视频异步推流消息。。。");
            byte[] body = messageExt.getBody();
            String JsonStr = new String(body, "utf-8");

            //json 字符串反转为实体类对象
            MediaFile mediaFile = JSON.parseObject(JsonStr, MediaFile.class);

            //开始使用ffmpeg推流到云服务器
            mediaFileService.handleFile2m3u8(mediaFile);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }
}
